package com.situ.mall.controller.back;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.situ.mall.entity.Result;
import com.situ.mall.entity.User;
import com.situ.mall.service.IUserService;

@Controller
@RequestMapping("/login")
public class LoginController {
	
	@Autowired
	private IUserService userService;

// 1.登录验证
	@RequestMapping("/login.action")
	@ResponseBody
	public Result login(User formUser, HttpSession session) {
		System.out.println("LoginController.login()");
		System.out.println(formUser);
		
		User user = userService.login(formUser);
		
		if (user==null || " ".equals(user)) {
			return Result.error("登陆失败，请检查账户密码");
		}else {
			session.setAttribute("user", user);
			return Result.success("登陆成功");
		}
	}
	
//2.退出登录，销毁session
	@RequestMapping("/loginout.action")
	public String loginout(HttpServletRequest req) {
		System.out.println("LoginController.loginout()");
		
		//从作用域拿session
		HttpSession session = req.getSession();
		
		//销毁
		session.invalidate();
		
		//销毁后 跳回到主页
		return "index";
	}
	
}
